package top.login;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import top.mapper.UserMapper;
import top.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

@WebServlet("/register")
public class Register extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                                            throws ServletException, IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if (username == null || "".equals(username) || password == null
                || "".equals(password)) {
            RegisterFail(resp);
        }else{
            //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new
                    SqlSessionFactoryBuilder().build(inputStream);

            //获取sqlSessionFactory对象,用他来执行sql
            SqlSession sqlSession = sqlSessionFactory.openSession();

            //执行sql

            //获取Mapper接口的代理对象
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            User user = userMapper.findUserByUserName(username);


            if (user != null) {
                RegisterEx(resp);
            }else {
                userMapper.addUser(username,password);
                sqlSession.commit();
                RegisterSu(resp);
            }
            //释放资源
            inputStream.close();
        }

    }

    private void RegisterEx(HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        resp.setHeader("content-type","text/html");
        writer.write("<h1>用户名已存在</h1>");
    }

    private void RegisterSu(HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        resp.setHeader("content-type","text/html");
        writer.write("<h1>注册成功</h1>");
    }

    private void RegisterFail(HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        resp.setHeader("content-type","text/html");
        writer.write("<h1>注册失败</h1>");
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                                            throws ServletException, IOException {
        doGet(req,resp);
    }
}
